package com.dream.model.app;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Type;

import com.dream.model.base.AppUserModelBase;

@Entity
@Table(name="t_app_group_resource")
public class AppGroupResource extends AppUserModelBase implements Serializable {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1697472830840031945L;

	@EmbeddedId
	private AppGroupResourcePK id;
	
	@Column(name="deleted")
	@Type(type = "org.hibernate.type.NumericBooleanType")
	private boolean deleted;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="resource_id",updatable=false,insertable=false)
	private AppResource appResource;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="group_id",updatable=false,insertable=false)
	private AppGroup appGroup;
	
	public AppGroup getAppGroup() {
		return appGroup;
	}

	public void setAppGroup(AppGroup appGroup) {
		this.appGroup = appGroup;
	}

	public AppResource getAppResource() {
		return appResource;
	}

	public void setAppResource(AppResource appResource) {
		this.appResource = appResource;
	}

	public AppGroupResourcePK getId() {
		return id;
	}

	public void setId(AppGroupResourcePK id) {
		this.id = id;
	}

	public boolean isDeleted() {
		return deleted;
	}

	public void setDeleted(boolean deleted) {
		this.deleted = deleted;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AppGroupResource other = (AppGroupResource) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	@Embeddable 
	public static class AppGroupResourcePK implements Serializable{
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		@Column(name="group_id")
		private String groupId;
		
		@Column(name="resource_id")
		private long resourceId;

		public String getGroupId() {
			return groupId;
		}

		public void setGroupId(String groupId) {
			this.groupId = groupId;
		}

		public long getResourceId() {
			return resourceId;
		}

		public void setResourceId(long resourceId) {
			this.resourceId = resourceId;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result
					+ ((groupId == null) ? 0 : groupId.hashCode());
			result = prime * result + (int) (resourceId ^ (resourceId >>> 32));
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			AppGroupResourcePK other = (AppGroupResourcePK) obj;
			if (groupId == null) {
				if (other.groupId != null)
					return false;
			} else if (!groupId.equals(other.groupId))
				return false;
			if (resourceId != other.resourceId)
				return false;
			return true;
		}
		
	}
	
}
